package com.base.mine;

import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.base.commonlib.base.CBaseBarMvpActivity;
import com.base.commonlib.bean.RoadEntity;
import com.base.commonlib.http.utils.SnackBarUtil;
import com.base.mine.adapter.BaseAdapter;
import com.base.mine.adapter.BaseHolder;
import com.base.mine.databinding.ActivityRoadBinding;
import com.base.mine.presenter.RoadPresenter;
import com.base.mine.view.RoadContract;

/**
 * Created by arvin on 2018/8/27.
 */
@Route(path = "/mine/road")
public class RoadActivity extends CBaseBarMvpActivity<RoadPresenter> implements RoadContract,View.OnClickListener {

    private ActivityRoadBinding mBinding;
    private RoadPresenter mPresenter;
    private BaseAdapter<RoadEntity> mAdapter;

    @Override
    protected void init() {
        super.init();
        mBinding = (ActivityRoadBinding) mRootBinding;
        setBackNavigation();
        setBarTitle("路段选择");
        setRigImg(R.drawable.ic_refresh_w, new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mBinding.search.etSearch.setText("");
                mPresenter.refresh();
            }
        });
        mBinding.fab.setOnClickListener(this);
        mBinding.search.tvCancel.setOnClickListener(this);
    }



    @Override
    public void showRoads() {
        mAdapter.notifyDataSetChanged();
    }



    public void iniRv() {
        mAdapter = new BaseAdapter<RoadEntity>(mPresenter.mRoads, mContext) {

            @Override
            public void convert(BaseHolder holder, RoadEntity road, boolean isHeader, boolean isBottom) {
                TextView tv = holder.getView(R.id.name);
                tv.setText(road.getRoadName());
                CheckBox cb = holder.getView(R.id.cb);
                if(mPresenter.mMineRoads.contains(road))
                {
                    cb.setChecked(true);
                }else {
                    cb.setChecked(false);
                }

            }
        };
        mAdapter.putNormalType(R.layout.item_road);
        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseHolder holder, int dataPosition, boolean isHeader, boolean isBottom) {
                CheckBox cb = holder.getView(R.id.cb);
                cb.performClick();
                if(cb.isChecked())
                {
                    mPresenter.mMineRoads.add(mPresenter.mRoads.get(dataPosition));
                }else
                {
                    mPresenter.mMineRoads.remove(mPresenter.mRoads.get(dataPosition));
                }

            }
        });
        mBinding.rv.setLayoutManager(new LinearLayoutManager(mContext));
        mBinding.rv.setAdapter(mAdapter);
    }

    @Override
    protected void start() {
        mPresenter = new RoadPresenter(this,this);
        iniRv();
        initSearch();
        mPresenter.start();
    }

    private void initSearch() {
        mBinding.search.etSearch.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                mBinding.search.tvCancel.setVisibility(View.VISIBLE);
            }

            @Override
            public void afterTextChanged(Editable s) {
                String keyWord = s.toString();
                if(TextUtils.isEmpty(keyWord))
                {
                    mBinding.search.tvCancel.setVisibility(View.GONE);
                }
                mPresenter.search(keyWord);


            }
        });
    }

    @Override
    public void showMes(String msg) {
        SnackBarUtil.showMsg(mBinding.fab,msg);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.fab) {
            mPresenter.setUserRoads();
            finish();

        } else if (i == R.id.tv_cancel) {
            mBinding.search.etSearch.setText("");

        }
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.activity_road;
    }



}
